package com.liuwa.kebendiandu;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.liuwa.common.UnitTestBase;
import com.liuwa.kebendiandu.model.Module;
import com.liuwa.kebendiandu.model.Word;
import org.apache.commons.io.FileUtils;
import org.apache.velocity.runtime.directive.Parse;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.rmi.ServerException;
import java.util.List;

public class Parser extends UnitTestBase {

    private static Logger logger = LoggerFactory.getLogger(Parser.class);

    @Test
    public void test() throws IOException {
        File dir = new File("E:\\kebendiandu\\book");

        for(File file : dir.listFiles()){
            if(!file.getName().endsWith(".txt")){
                continue;
            }

            logger.info("book {}", file.getName());
            String content = FileUtils.readFileToString(file, StandardCharsets.UTF_8);

            JSONObject json = JSONObject.parseObject(content);
            List<Module> modules = JSONArray.parseArray(json.getJSONObject("data").getString("list"), Module.class);
            for(Module module : modules){
                for(Word word : module.getWords()){
                    download(word.getAudio());
                    download(word.getImage());
                    download(word.getSampleSentenceAudio());
                }
            }
        }




    }

    public static void main(String[] args) throws IOException {
        File dir = new File("E:\\kebendiandu\\book");

        File fltrpFile = new File("E:\\kebendiandu\\book\\fltrp.json");
        JSONArray books = JSONArray.parseArray(FileUtils.readFileToString(fltrpFile, StandardCharsets.UTF_8));
        int i = 0;
        for(File file : dir.listFiles()){
            if(!file.getName().endsWith(".txt")){
                continue;
            }

            JSONObject book = books.getJSONObject(i++);


            logger.info("book {}", file.getName());
            String content = FileUtils.readFileToString(file, StandardCharsets.UTF_8);

            JSONObject json = JSONObject.parseObject(content);
            List<Module> modules = JSONArray.parseArray(json.getJSONObject("data").getString("list"), Module.class);
            JSONArray moduleArray = book.getJSONArray("modules");
            if(modules.size() != moduleArray.size()){
                throw new ServerException("数据不匹配");
            }
            int index = 0;
            for(Module module : modules){
                JSONObject moduleData = moduleArray.getJSONObject(index ++);
                for(Word word : module.getWords()){
                    word.setAudio(rename(word.getAudio()));
                    word.setImage(rename(word.getImage()));
                    word.setSampleSentenceAudio(rename(word.getSampleSentenceAudio()));

                }
                moduleData.put("words", module.getWords());
            }
        }
        System.out.println(books);
    }

    public static String rename(String url){
        if(url == null){
            return null;
        }
        url = url.split("\\?")[0];
        url = url.replace("https://cdn.beijinxuetang.com/uploads/vocabulary/", "");
        return url;
    }

    public static File download(String url){
        if(url == null){
            logger.error("url is null");
            return null;
        }
        try{
            String fileName = "E:\\kebendiandu\\material\\" + url.split("\\?")[0].replace("https://", "");
            File file = new File(fileName);
            if(file.exists()){
                return file;
            }
            logger.info("download url {}", url);
            FileUtils.copyURLToFile(new URL(url), file);
            return file;
        }
        catch (FileNotFoundException ex){
            logger.error("文件不存在 {}", url);
            return null;
        }
        catch (IOException ex){
            logger.error(ex.getMessage(), ex);
        }
        return null;
    }
}
